package session

import (
	"xrt/components/http/session/sess"
	_ "xrt/components/http/session/sess/redis"
	"xrt/utils"
	"xrt/utils/file/log"
)

/**
 * 使用第三方beego框架的session管理器
 * session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
 * session.NewManager("file", `{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"./tmp"}`)
 * session.NewManager("redis", `{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"db"}`)
 * redis存储session时，ProviderConfig配置格式为：db
 */
func InitSession(configs map[string]string) *session.Manager {
	sessionStore := configs["session_store"]
	sessionName := configs["session_name"]
	sessionLifeTime := utils.ParseInt(configs["seesion_life_time"])
	sessionProviderConfig := configs["session_provider_config"]
	sessionIdLength := utils.ParseInt(configs["seesion_id_length"])
	sessionDisableHTTPOnly := utils.ParseBool(configs["session_disable_httponly"])
	sessionEnableSetCookie := utils.ParseBool(configs["session_enable_set_cookie"])
	sessionConfig := &session.ManagerConfig{
		CookieName:      sessionName,
		Gclifetime:      int64(sessionLifeTime),
		ProviderConfig:  sessionProviderConfig,
		SessionIDLength: int64(sessionIdLength),
		DisableHTTPOnly: sessionDisableHTTPOnly,
		EnableSetCookie: sessionEnableSetCookie,
	}

	if globalSessions, err := session.NewManager(sessionStore, sessionConfig); err != nil {
		log.Critical("Session Manager Initial Failed.")
	} else {
		go globalSessions.GC()
		return globalSessions
	}
	return nil
}
